#include "mprpcapplication.h"

#include <iostream>
#include <unistd.h>
#include <string>

MprpcConfig MprpcApplication::mprpcConfig_;

void ShowArgsHelp()
{
    std::cout << "format: command -i <configfile>" << std::endl;
}

void MprpcApplication::init(int argc, char *argv[])
{
    if (argc < 2)
    {
        ShowArgsHelp();
        exit(EXIT_FAILURE);
    }

    int c = 0;
    std::string configFile;
    while ((c = getopt(argc, argv, "i:")) != -1)
    {
        switch (c)
        {
        case 'i':
            configFile = optarg;
            break;
        case '?':
            ShowArgsHelp();
            exit(EXIT_FAILURE);
        case ':':
            ShowArgsHelp();
            exit(EXIT_FAILURE);
        default:
            break;
        }
    }

    // TODO:加载配置文件
    mprpcConfig_.loadConfigFile(configFile.c_str());
    // std::cout << "rpcserverip:" << mprpcConfig_.load("rpcserverip") << std::endl
    //           << "rpcserverport:" << mprpcConfig_.load("rpcserverport") << std::endl
    //           << "zookeeperip:" << mprpcConfig_.load("zookeeperip") << std::endl
    //           << "zookeeperport:" << mprpcConfig_.load("zookeeperport") << std::endl;
}
MprpcApplication &MprpcApplication::getInstance()
{
    static MprpcApplication app;
    return app;
}

MprpcConfig& MprpcApplication::getConfig() {
    return mprpcConfig_;
}